<?php
	class Post {
		
		private $id;
		private $title;
		private $content;
		
		function getId() {
			return $this->id;
		}
		
		function getTitle() {
			return $this->title;
		}
		
		function setTitle($title) {
			$this->title = $title;
			$this->update();
		}
		
		function getContent() {
			return $this->title;
		}
		
		function setContent($content) {
			$this->content = $content;
			$this->update();
		}
		
		static function exists($title) {
			$val;
			$temp = $title;
			require "conn.php";
			$rs = $conn->query("select id from posts where title='".(string)$temp."'");
			if ($rs->num_rows == 0) {
				$val .= " style='color: red;' ";
			}
			require "conn_close.php";
			return $val;
		}
		
		static function fetch($title) {
			$val;
			require "conn.php";
			require "util.php";
			$rs = $conn->query("select id, title, content from posts where title='".(string)$title."'");
			if ($rs->num_rows == 0) {
				$val = "NONE";
			} else {
				$val = "{";
				while ($r = $rs->fetch_array()) {
					$val .= "\"id\": \"".$r["id"]."\", ";
					$val .= "\"title\": \"".mn_jsonify(htmlspecialchars($r["title"]))."\", ";
					$content = mn_format($r["content"]);
					$val .= "\"content\": \"".mn_jsonify($content)."\", ";
					$val .= "\"source\": \"".mn_jsonify($r["content"])."\" ";
				}
				$val .= "}";
			}
			require "conn_close.php";
			return $val;
		}
		
		static function history_view($id) {
			$val;
			require "conn.php";
			require "util.php";
			$rs = $conn->query("select id, date, content from actions where id='".(int)$id."'");
			if ($rs->num_rows == 0) {
				$val = "NONE";
			} else {
				$val = "{";
				while ($r = $rs->fetch_array()) {
					$val .= "\"id\": \"".$r["id"]."\", ";
					$val .= "\"date\": \"".$r["date"]."\", ";
					$val .= "\"title\": \"".mn_jsonify(htmlspecialchars($r["title"]))."\", ";
					$content = mn_format($r["content"]);
					$val .= "\"content\": \"".mn_jsonify($content)."\"";
				}
				$val .= "}";
			}
			require "conn_close.php";
			return $val;
		}
		
		static function children($title) {
			$level = count(explode("/", $title));
			$val;
			require "conn.php";
			require "util.php";
                        $rs = $conn->query("select id, title from posts where title like '".(string)$title."/%' order by title");
			if ($rs->num_rows == 0) {
				$val = "NONE";
			} else {
				$val = "[";
				$i = 0;
				while ($r = $rs->fetch_array()) {
					$i++;
					if (count(explode("/", $r["title"])) == ($level+1)) {
						$val .= "{";
						$val .= "\"id\": \"".$r["id"]."\", ";
						$val .= "\"title\": \"".mn_jsonify(htmlspecialchars($r["title"]))."\"";
						$val .= "}";
						if ($i < $rs->num_rows) {
							$val .= ",";
						}
					}					
				}
				$val .= "]";
			}
			require "conn_close.php";
			return $val;
		}
		
		static function search($title) {
			$q = explode(" ", $title);
			$val;
			require "conn.php";
			require "util.php";
			$sql = "select id, title, content from posts where title like '%".(string)$title."%' ";
			for ($i=0; $i<count($q);$i++) {
				$sql .= " or title like '%".(string)$q[$i]."%' ";
			}
			
			// $sql .= " or content like '%".(string)$title."%'";
			
			$rs = $conn->query($sql);
			if ($rs->num_rows == 0) {
				$val = "NONE";
			} else {
				$val = "[";
				$i = 0;
				while ($r = $rs->fetch_array()) {
					$i++;
					$val .= "{";
					$val .= "\"id\": \"".$r["id"]."\", ";
					$val .= "\"title\": \"".mn_jsonify(htmlspecialchars($r["title"]))."\"";
					$val .= "}";
					if ($i < $rs->num_rows) {
						$val .= ",";
					}
				}
				$val .= "]";
			}
			require "conn_close.php";
			return $val;
		}
		
		static function create($user, $title, $content) {
			require "conn.php";
			$conn->query("insert into posts (title, content) ".
								"values ('".(string)$title."','"
								.(string)$content."')");
			$conn->query("insert into posts (title, content) ".
								"values ('Хэлэлцүүлэг:".(string)$title."','<strong>"
								.(string)$title."</strong> хэлэлцүүлэг')");
			require "constant.php";
			$conn->query("insert into actions (type, user, title, content, date) ".
								"values ('".ACTION_CREATEPOST."', '".(string)$user."','"
								.(string)$title."','".(string)$content."',SYSDATE())");
			$conn->query("insert into actions (type, user, title, content, date) ".
								"values ('".ACTION_AUTOCREATEPOST."', '".(string)$user."','Хэлэлцүүлэг:"
								.(string)$title."','<strong>".(string)$title."</strong> хэлэлцүүлэг',SYSDATE())");
			require "conn_close.php";
		}
		
		static function undo($title, $id, $user) {
			require "conn.php";
			$rs = $conn->query("select content from actions where title='".(string)$title."' and id='".(int)$id."'");
			if ($rs->num_rows == 1) {
				while ($r = $rs->fetch_array()) {
					$conn->query("update posts set content= '"
										.(string)$r["content"]."' where ".
										"title='".(string)$title."'");
					require "constant.php";
					$conn->query("insert into actions (type, user, title, content, date) ".
										"values ('".ACTION_EDITPOST."', '".(string)$user."','"
										.(string)$title."','".(string)$r["content"]."',SYSDATE())");
				} // ".(string)$_SESSION["mnwiki_user"]."
			}			
			require "conn_close.php";
		}
		
		static function edit($user, $title, $content) {
			require "conn.php";
			$conn->query("update posts set content= '"
								.(string)$content."' where ".
								"title='".(string)$title."'");
			require "constant.php";
			$conn->query("insert into actions (type, user, title, content, date) ".
								"values ('".ACTION_EDITPOST."', '".(string)$user."','"
								.(string)$title."','".(string)$content."',SYSDATE())");
			require "conn_close.php";
		}
		
		static function history($title) {
			$val;
			require "conn.php";
                        require "util.php";
			$sql = "select a.id as id, a.title as title, a.date as date, a.user as user, length(a.content) as size from actions a, posts p where a.title='".(string)$title."' and p.title=a.title order by date desc";
			$rs = $conn->query($sql);
			if ($rs->num_rows == 0) {
				$val = "NONE";
			} else {
				$val = "[";
				$i = 0;
				while ($r = $rs->fetch_array()) {
					$i++;
					$val .= "{";
					$val .= "\"id\": \"".$r["id"]."\", ";
					$val .= "\"date\": \"".$r["date"]."\", ";
					$val .= "\"size\": \"".$r["size"]."\", ";
					$val .= "\"user\": \"".mn_jsonify(htmlspecialchars($r["user"]))."\", ";
					$val .= "\"title\": \"".mn_jsonify(htmlspecialchars($r["title"]))."\"";
					$val .= "}";
					if ($i < $rs->num_rows) {
						$val .= ",";
					}
				}
				$val .= "]";
			}
			require "conn_close.php";
			return $val;
		}
		
		static function recent($lower, $higher) {
			$val;
			require "conn.php";
                        require "util.php";
			$sql = "select type, title, date, user, length(content) as size from actions order by date desc limit ".$lower.",".$higher."";
			$rs = $conn->query($sql);
			if ($rs->num_rows == 0) {
				$val = "NONE";
			} else {
				$val = "[";
				$i = 0;
				while ($r = $rs->fetch_array()) {
					$i++;
					$val .= "{";
					$val .= "\"type\": \"".$r["type"]."\", ";
					$val .= "\"date\": \"".$r["date"]."\", ";
					$val .= "\"size\": \"".$r["size"]."\", ";
					$val .= "\"user\": \"".mn_jsonify(htmlspecialchars($r["user"]))."\", ";
					$val .= "\"title\": \"".mn_jsonify(htmlspecialchars($r["title"]))."\"";
					$val .= "}";
					if ($i < $rs->num_rows) {
						$val .= ",";
					}
				}
				$val .= "]";
			}
			require "conn_close.php";
			return $val;
		}
		
	}
?>
